package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.Client;
import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.shared.data.OrderPayment;
import com.emex.app.shared.data.Orders;
import com.emex.app.shared.data.enums.PayMethod;
import com.emex.app.stuff.provider.ClientDataProvider;
import com.emex.app.stuff.provider.JuridicalPersonDataProvider;
import com.emex.app.stuff.provider.OrderDataProvider;
import com.emex.app.stuff.utils.StringUtilities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class OrderPaymentReportTableModel extends AbstractTableModel
        implements TableModel {

    /**
     * List of all orderPayments.
     */
    private Map<OrderPayment, String> orderPaymentFio = new HashMap<>();

    private final List<OrderPayment> orderPayments;

    private Double genQiwi = 0.0;

    private Double genNal = 0.0;

    private Double genBeznal = 0.0;

    private Double genCard = 0.0;

    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Название", "ФИО клиента", "Наличные", "Без.нал.", "QIWI", "Карта", "Комментарий"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class, String.class, Double.class, Double.class, Double.class, Double.class, String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public OrderPaymentReportTableModel(List<OrderPayment> orderPaymentsList) {
        if (null == orderPaymentsList) {
            orderPaymentsList = new ArrayList<>();
        }
        orderPayments = orderPaymentsList;
        initializeMap();
        calculateSums();
    }

    private void initializeMap() {
        if (orderPayments != null && !orderPayments.isEmpty()) {
            for (OrderPayment op : orderPayments) {
                Client client = null;
                if (null != op.getOrderId()) {
                    Orders o = OrderDataProvider.getInstance().selectOrderByPrimaryKey(op.getOrderId());
                    if (o.getClientId() != null) {
                        client = ClientDataProvider.getInstance().selectByPrimaryKey(o.getClientId());
                        orderPaymentFio.put(op, StringUtilities.getClientFIO(client));
                    } else {
                        JuridicalPerson jp = JuridicalPersonDataProvider.getInstance().selectJuridicalPersonByPrimaryKey(o.getJurClienId());
                        orderPaymentFio.put(op, jp.getName());
                    }
                } else if (null != op.getClientId()) {
                    client = ClientDataProvider.getInstance().selectByPrimaryKey(op.getClientId());
                    orderPaymentFio.put(op, StringUtilities.getClientFIO(client) + "(Интернет)");
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return orderPayments.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < orderPayments.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return orderPayments.get(rowIndex);
                }
                case 0: {
                    return orderPayments.get(rowIndex).getName();
                }
                case 1: {
                    return orderPaymentFio.get(orderPayments.get(rowIndex));
                }
                case 2: {
                    PayMethod payMethod = PayMethod.valueOf(orderPayments.get(rowIndex).getPayMethod());
                    if (payMethod.equals(PayMethod.CASH)) {
                        return orderPayments.get(rowIndex).getSumma();
                    } else {
                        return "";
                    }
                }
                case 3: {
                    PayMethod payMethod = PayMethod.valueOf(orderPayments.get(rowIndex).getPayMethod());
                    if (payMethod.equals(PayMethod.CARD)) {
                        return orderPayments.get(rowIndex).getSumma();
                    } else {
                        return "";
                    }
                }
                case 4: {
                    PayMethod payMethod = PayMethod.valueOf(orderPayments.get(rowIndex).getPayMethod());
                    if (payMethod.equals(PayMethod.QIWI)) {
                        return orderPayments.get(rowIndex).getSumma();
                    } else {
                        return "";
                    }
                }
                case 5: {
                    PayMethod payMethod = PayMethod.valueOf(orderPayments.get(rowIndex).getPayMethod());
                    if (payMethod.equals(PayMethod.KARTA)) {
                        return orderPayments.get(rowIndex).getSumma();
                    } else {
                        return "";
                    }
                }
                case 6:
                    return orderPayments.get(rowIndex).getComment();
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    private void calculateSums() {
        if (null != orderPayments && !orderPayments.isEmpty()) {
            for (OrderPayment op : orderPayments) {
                PayMethod payMethod = PayMethod.valueOf(op.getPayMethod());
                switch (payMethod) {
                    case CARD:
                        genBeznal += op.getSumma();
                        break;
                    case CASH:
                        genNal += op.getSumma();
                        break;
                    case KARTA:
                        genCard += op.getSumma();
                        break;
                    case QIWI:
                        genQiwi += op.getSumma();
                        break;
                }
            }

        }
    }

    public Double getGenQiwi() {
        return genQiwi;
    }

    public Double getGenNal() {
        return genNal;
    }

    public Double getGenBeznal() {
        return genBeznal;
    }

    public Double getGenCard() {
        return genCard;
    }

}
